using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class NPCMgr : MonoBehaviour
{
    [Serializable]
    public class SpawnInfo
    {
        public int gridX;
        public int gridY;
        public int taskId;
        public string entityKey;
    }

    public SpawnInfo[] spawnInfos;
    private DialogueTrigger _dialogueTrigger;
    private List<NPC> npcList;
    // Start is called before the first frame update
    void Start()
    {
        //生成NPC
        npcList = new List<NPC>();
        for (int i = 0; i < spawnInfos.Length; i++)
        {
            SpawnInfo info = spawnInfos[i];
            XRes.Instantiate(info.entityKey, (obj) => {
                obj.transform.SetParent(transform);
                obj.transform.position = MapMix.GetPos(info.gridX, info.gridY);
                var npc = obj.GetComponent<NPC>();
                npc.SetTaskId(info.taskId);
                npcList.Add(npc);

                //实例化任务对应的道具 (临时放这里）
                GenerateTaskItems(info.taskId);
            });
        }

        _dialogueTrigger = new DialogueTrigger();
    }

    public static NPCMgr Instance;
    // Update is called once per frame
    private void Awake()
    {
        Instance = this;
    }

    private void OnEnable()
    {
        Facade.Instance.RegistEvent(GameEvent.Move.PosChange, OnHandePosChange);
    }

    private void OnDisable()
    {
        Facade.Instance.UnRegistEvent(GameEvent.Move.PosChange, OnHandePosChange);
    }

  
    private object OnHandePosChange(object sender)
    {
        #region 获取最近的NPC，然后检测显示/隐藏 对话框
        _dialogueTrigger.entityPlayer = Player.Instance;
        var playerTrans = _dialogueTrigger.entityPlayer.transform;
        float minDistance = float.MaxValue;
        float distance;
        NPC npcMin = null;
        foreach (var npc in npcList)
        {
            distance = Vector3.Distance(npc.transform.position, playerTrans.position);
            if(distance < minDistance)
            {
                minDistance = distance;
                npcMin = npc;
            }
        }
        if (npcMin == null)
            Debug.LogError("npc = nil");

        _dialogueTrigger.entityNPC = npcMin;
        _dialogueTrigger.Detect();
        #endregion
        return null;
    }

    //实例化任务对应的道具(临时放这里）
    private void GenerateTaskItems(int vTaskId)
    {
        var configTask = ConfigTask.GetElement(vTaskId);
        if (configTask.Type != TaskType.Item)
        {
            Debug.LogError("Can not handle Task Type:" + configTask.Type);
            return;
        }
        var itemId = configTask.Key;
        var itemPosList = configTask.SpawnPosList;
        var configItem = ConfigItem.GetElement(itemId);
        var itemPrefabName = configItem.PrefabName;
        foreach (var itemPos in itemPosList)
        {
            XRes.Instantiate(itemPrefabName, (vGameObject) => {
                vGameObject.transform.SetParent(null);//临时代码
                vGameObject.transform.position = itemPos;
                vGameObject.name = itemPrefabName;
                //Debug.Log("itemPrefabName:" + itemPrefabName + " itemPos:" + itemPos + " vTaskId:" + vTaskId);
            });
        }
    }
}
